<?php
// Neutrino GNU GPL non-mysql php cms-blog-engine
// © Zorg <ekumena@gmail.com, http://oiku.net.ru>, 2007-2008

header('Content-Type: text/html; charset=utf-8');
define('VERSION', 'Neutrino 0.751 (b300608)');

$_v = $_c = $_lk = $posts = array();
foreach ($_GET as $k => $v)  $_v[$k] = $v;
foreach ($_POST as $k => $v) $_v[$k] = $v;
foreach ($_COOKIE as $k => $v) $_c[$k] = $v;

$_lk['act']='act';
$_lk['pg']='pg';
$_lk['pt']='p';
$_lk['pgnw']=$_lk['pged']='pgnw';
$_lk['pgdel']='pgdel';
$_lk['ptnw']=$_lk['pted']='ptnw';
$_lk['ptdel']='ptdel';
$_lk['cmtdel']='cmtdel';
$_lk['cmt']='cmt';
$_lk['cmted']='cmted';

$_s['i']='';
if(@$incne) $_s['i']=$incne.'/';

$_s['core_dir']=$_s['i'].'core';
$_s['data_dir']=$_s['i'].'data';
$_s['files_dir']=$_s['i'].'files';
$_s['pages_dir']=$_s['i'].'data/pages';
$_s['ids_dir']=$_s['i'].'data/ids';
$_s['post_dir']=$_s['i'].'data/posts';
$_s['comm_dir']=$_s['i'].'data/comments';
$_s['sav_dir']=$_s['i'].'data/save';
$_s['tpl_dir']=$_s['i'].'data/pages';

$_s['tpl_head']='n.tpl.head';
$_s['tpl_comment']='n.tpl.comment';
$_s['tpl_acomment']='n.tpl.comment_a';
$_s['tpl_commform']='n.tpl.comment_form';
$_s['tpl_footer']='n.tpl.footer';
$_s['tpl_post']='n.tpl.post';
$_s['tpl_postform']='n.tpl.post_form';

$_s['pglinks']=$_tpa['pttl']=$pages=$_s['nocmt']=$cmnt['menu']=$post['menu']=$_s['mpg']=$post['brchk']=$_tpa['ptfup']=$_tpa['ptup']=$_tpa['ptdw']=$_tpa['ptfdw']=$_s['cdel']=$clcmt=$cmnt['del']=$_tpa['cmtup']=$_tpa['cmtdw']=$_tpa['cmtfup']=$_tpa['cmtfdw']='';

include_once($_s['pages_dir'].'/n.s.settings');

if(!$_s['base_url']) $_s['base_url']=$_SERVER['PHP_SELF'];
$_s['blogname']=$_s['title'];

function getapts() {
 global $_s;
	$d = dir($_s['post_dir']);
	while (false !== ($entry = $d->read())) if ($entry != '.' && $entry != '..') $pts[] = $entry;
	$d->close();
 return @$pts;
}

function cd($f=false) {
 global $_s, $_l;
	if($f) $f='['.$f.']';
	if($_s['delden']) return 'onClick="return confirm(\''.$_l['confirm'].' '.$f.'\')"';
 return 0;
}

function autoload() {
 global $_s, $exts;
	$d = dir($_s['core_dir'].'/ext');
	if($_s['exton']) while (false !== ($entry = $d->read())) if ($entry != '.' && $entry != '..') $exts[] = $entry;
	if(sizeof($exts)>0) sort($exts);
	$d->close();
}

function getallpage() {
 global $_s, $pages, $post;
	$d = dir($_s['pages_dir']);
	while (false !== ($entry = $d->read())) if ($entry != '.' && $entry != '..') $pages[] = $entry;
	$d->close();
}

function e($e) {
	echo $e;
}

function admenu($idpo) {
 global $post, $_lk, $_s, $_l, $_tpl;
	$post['edit']=$post['del']='';
	$post['menu']='';
	if (is_admin()) $post['menu'] = '[<a href="'.ptedlink($idpo).'">'.$_l['ed'].'</a>-<a href="'.ptdelink($idpo).'" '.cd($post['title']).'>'.$_l['del'].'</a>'.@$_tpl['mmpt'].']';
 return $post;
}

function block($bk) {
 global $_s, $_v, $post, $_lk, $_l, $_tpl;
	if(is_admin() && $_s['menublock']) e($_s['bkmenu']='[<a href='.pgedlink('n.b.'.$bk).'>'.$_l['ed'].'</a>-<a href='.pgdelink('n.b.'.$bk).' '.cd($bk).'>'.$_l['del'].'</a>'.@$_tpl['mmbk'].']');
	include($_s['pages_dir'].'/n.b.'.$bk);
}

function br() {
 global $post, $_s;
	if($_s['br'] && empty($post['nobr'])) $post['text'] = str_replace("\n", '<br>', $post['text']);
 return $post;
}

function post_info($p) {
 global $_s, $_l, $buffer, $_v;
	if(isset($buffer[$p]['id'])) foreach ($buffer[$p] as $k => $v) $post[$k] = $v;
    $inc=0;
    if(trim($p) && file_exists($_s['post_dir'].'/'.$p)) {
    	$inc=1;
    	$post['time'] = $p;
    	include_once($_s['post_dir'].'/'.$p);
    	$p=$post['id'];
    }
    if (!file_exists($_s['ids_dir'].'/'.$p) || !trim($p)) return 0;
    include_once($_s['ids_dir'].'/'.$p);
    if (!isset($post['ida']) || !file_exists($_s['post_dir'].'/'.$post['ida'])) return 0;
    if(!$inc && isset($post['ida']) && trim($post['ida'])) include_once($_s['post_dir'].'/'.$post['ida']);
	if(!isset($post['time'])) $post['time'] = $post['ida'];
	$post['timestamp'] = $post['ida'];
    $post['date'] = date($_s['dateformat'], $post['ida'] + $_s['time_offset'] * 3600);
	if($post['hidden'] && !@$_v['p']) $post['close']=1;
	if($post['close'] && !is_admin() && @$_s['name']!=$post['author']) return 0;
    if (!file_exists($_s['comm_dir'].'/'.$post['id'])) $post['c_count'] = 0;
    else @$post['c_count'] = sizeof(file($_s['comm_dir'].'/'.$post['id']));
	if(!isset($buffer[$p]['id'])) foreach ($post as $k => $v) $buffer[$p][$k] = $v;
 return $post;
}

function genid() {
	global $_s;
		$id=sizeof(getapts())+1;
		while(file_exists($_s['ids_dir'].'/'.$id)) $id++;
 return $id;
}

function redirect($u) {
	global $_l;
	  e('<meta http-equiv="refresh" content="4;url='.$u.'"><center>'.$_l['wait'].'<br>[ <a href='.$u.'>'.$_l['dwait'].'</a> ]</center>');
}

function is_admin() {
 global $_s, $_c, $user, $_v, $_l;
    if (isset($_c['sid']) && $_c['sid'] ==  md5($_s['pass'])) return 1;
    else if (function_exists('is_user')) return is_user();
    else return 0;
}

function plink($p) {
 global $_s, $_lk;
    if (function_exists('hook_plink')) return hook_plink($p);
 return $_s['base_url'].'?'.$_lk['pt'].'='.$p;
}

function ptedlink($p) {
 global $_s, $_lk;
    if (function_exists('hook_ptedlink')) return hook_ptedlink($p);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['pted'].'&'.$_lk['pt'].'='.$p;
}

function ptdelink($p) {
 global $_s, $_lk;
    if (function_exists('hook_ptdelink')) return hook_ptdelink($p);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['ptdel'].'&'.$_lk['pt'].'='.$p;
}

function cmtdelink($ci,$po) {
 global $_s, $_lk;
    if (function_exists('hook_cmtdelink')) return hook_cmtdelink($ci,$po);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['cmtdel'].'&c='.$ci.'&'.$_lk['pt'].'='.$po;
}

function cmtedlink($ci,$po) {
 global $_s, $_lk;
    if (function_exists('hook_cmtedlink')) return hook_cmtedlink($ci,$po);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['cmted'].'&c='.$ci.'&'.$_lk['pt'].'='.$po;
}

function alink($al) {
 global $_s, $_lk;
    if (function_exists('hook_alink')) return hook_alink($al);
 return $_s['base_url'].'?'.$_lk['act'].'='.$al;
}

function pglink($page_id) {
 global $_s, $_lk;
    if (function_exists('hook_pglink')) return hook_pglink($page_id);
 return $_s['base_url'].'?'.$_lk['pg'].'='.$page_id;
}

function pgdelink($pg) {
 global $_s, $_lk;
    if (function_exists('hook_pgdelink')) return hook_pgdelink($pg);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['pgdel'].'&'.$_lk['pg'].'='.$pg;
}

function pgedlink($pg) {
 global $_s, $_lk;
    if (function_exists('hook_pgedlink')) return hook_pgedlink($pg);
 return $_s['base_url'].'?'.$_lk['act'].'='.$_lk['pged'].'&'.$_lk['pg'].'='.$pg;
}

function skiplink($p) {
 global $_s, $_lk;
    if (function_exists('hook_skiplink')) return hook_skiplink($p);
 return $_s['base_url'].'?skip='.$p;
}

function pgselink($pg) {
 global $_lk, $_s;
    if (function_exists('hook_pgselink')) return hook_pgselink($pg);
 return $_s['base_url'].'?'.$_lk['pg'].'='.$pg.'&set=1';
}

function is_sett() {
 global $_v, $_s, $_lk;
    if(is_admin() && isset($_v['set']) && $_v['set']==1 && eregi($_s['plname'],$_v[$_lk['pg']])) return 1;
    else return 0;
}

function fsave($fle,$mod,$txt) {
    $fh = fopen($fle, $mod);
    flock($fh, LOCK_EX);
    fwrite($fh, $txt);
    flock($fh, LOCK_UN);
    fclose($fh);
    @chmod($fle, 0777);
}

function prp($t) {
 return str_replace(array('\\','\''),array('\\\\','\\\''),$t);
}

function bbcode($t) {
 global $_s;
    $bk=explode(',',$_s['bbcomm']);
    foreach ($bk as $b) { $b=trim($b); $t = str_replace(array('['.$b.']','[/'.$b.']'),array('<'.$b.'>','</'.$b.'>'), $t); }
    $t = preg_replace('/(\[url\])(.+)(\[\/url\])/', '<a href="\\2">\\2</a>', $t);
 return $t;
}

function f($fu,$di) {
 global $_s, $_l, $_v, $_c, $_lk, $post, $posts, $_tpa, $_tpl, $pages, $user, $cmnt;
    include($_s['core_dir'].'/func/'.$fu);
}

function blog() {
    f('blog','');
}

function page($p) {
    f('page',$p);
}

function post($p) {
    f('post',$p);
}

if(!isset($_s['engineoff'])) {

if(isset($_FILES['nefis']['name'])) for ($i=0; $i<sizeof($_FILES['nefis']['name']); $i++) if(trim($_FILES['nefis']['tmp_name'][$i])) @move_uploaded_file($_FILES['nefis']['tmp_name'][$i], $_s['files_dir'].'/'.strtolower(basename($_FILES['nefis']['name'][$i])));

if(is_admin()) $_s['name'] = $_s['authorname'];

if (get_magic_quotes_gpc()) foreach($_v as $k => $v) $_v[$k] = stripslashes($v);

$_v[$_lk['act']] = (isset($_v[$_lk['act']]) ? $_v[$_lk['act']] : '');

if ($_s['savecmt'] && $_v[$_lk['act']]==$_lk['cmt']) {
@setcookie('nenick', $_v['nick'], time()+$_s['stime']);
@setcookie('newww', $_v['www'], time()+$_s['stime']);
@setcookie('nemail', $_v['mail'], time()+$_s['stime']);
}

if (!isset($exts) && $_s['exton']) autoload();
if (isset($exts) && sizeof($exts)>0) foreach ($exts as $entry) if (!strstr($entry, '.off')) include_once($_s['core_dir'].'/ext/'.$entry);

if(@$_v['sw'] == $_s['pass']) {
setcookie('sid', md5($_s['pass']), time() + $_s['stime'], '/');
header('Location: '.$_s['afterlogin']);
}
if (is_admin() && isset($_v['lgout'])) { setcookie('sid', '', time()-3600, '/'); header('Location: '.$_SERVER['PHP_SELF']); }

if (isset($_v[$_lk['pt']]) && $p=post_info($_v[$_lk['pt']])) if(trim($p['title'])) $_s['title']=$p['title'].' - '.$_s['title'];

include($_s['tpl_dir'].'/'.$_s['tpl_head']);

if(isset($_v[$_lk['act']]) && trim($a=$_v[$_lk['act']])) {
if(file_exists($_s['core_dir'].'/act/'.$a) && !strstr($a,'.')) include($_s['core_dir'].'/act/'.$a);
} elseif(isset($_v[$_lk['pg']])) f('page',$_v[$_lk['pg']]);
elseif (isset($_v[$_lk['pt']])) f('post',$_v[$_lk['pt']]);
elseif ($_s['start_page']) f('page',$_s['start_page']);
else blog();

include($_s['tpl_dir'].'/'.$_s['tpl_footer']);
}
?>